# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=keras
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.3.1
pkgrel=1
pkgdesc="Keras is a high-level neural networks API, written in Python and capable of running on top of TensorFlow, CNTK, or Theano (mingw-w64)"
arch=('any')
url="https://keras.io/"
license=('MIT')
makedepends=("patch"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/keras-team/keras/archive/${pkgver}.tar.gz
        0001-change-backend-to-theano.patch)
sha256sums=('7e8df9c5291f21a01bccffd8cbdd44abd7d2679ab2f7a892a503e88291627183'
            '2e53d3c474d0d797ef7c6faf3ecdc77dfe123fa43bd19a151caaf894baadb47e')

_deps=('numpy' 'scipy' 'six' 'yaml' 'h5py' 'keras_applications' 'keras_preprocessing' 'theano')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -b -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-change-backend-to-theano.patch
}

build() {
  cd "${srcdir}"
  rm -rf python{2,3}-build
  for builddir in python{2,3}-build; do
    cp -r ${_realname}-${pkgver} ${builddir}
    pushd $builddir
    ${MINGW_PREFIX}/bin/${builddir%-build} setup.py --quiet build
    popd
  done
}

package_python3-keras() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3" "${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python3-}")

  cd "${srcdir}/python3-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py --quiet install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
}

package_python2-keras() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2" "${_deps[@]/#/${MINGW_PACKAGE_PREFIX}-python2-}")

  cd "${srcdir}/python2-build"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py --quiet install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}

package_mingw-w64-i686-python2-keras() {
  package_python2-keras
}

package_mingw-w64-i686-python3-keras() {
  package_python3-keras
}

package_mingw-w64-x86_64-python2-keras() {
  package_python2-keras
}

package_mingw-w64-x86_64-python3-keras() {
  package_python3-keras
}
